<script type="application/javascript">

    // +---------------------------------------------
    // 搜索
    // +---------------------------------------------
    function _search(table, where) {
        //执行重载
        table.reload('testReload', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: where
        }, 'data');
    }


    // +---------------------------------------------
    // 重新载入
    // +---------------------------------------------
    function heavy_load($) {
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    }


    // +---------------------------------------------
    // 监听行工具事件
    // +---------------------------------------------
    function _tool(obj, width = '45%', height = '50%') {
        var data = obj.data //获得当前行数据
            , layEvent = obj.event; //获得 lay-event 对应的值

        if (layEvent === 'detail') {    // 详情

            let url = "{:url('info')}";
            let params = {id: data.id};
            _axios(url, params)

        } else if (layEvent === 'del') {    // 删除

            layer.confirm('真的删除行么', function (index) {
                //向服务端发送删除指令
                let url = "{:url('delete')}";
                let params = {id: data.id};
                _del(url, params, obj)
            });

        } else if (layEvent === 'edit') {   // 修改
            let content = "update?id=" + data.id
            _open('编辑', content, width, height)

        } else if (layEvent === 'createlm') {   // 修改

            let content = "createlm?id=" + data.id
            _open('添加栏目', content, width, height)
        }else if (layEvent === 'many_upload') {   // 车型图片【多图上传】

            let content = "many_upload?id=" + data.id
            _open('多图上传', content, "50%", "70%")
        }
    }

    // +---------------------------------------------
    // 监听头工具栏事件
    // +---------------------------------------------
    function _toolbar(obj, table, width = '45%', height = '50%') {
        var checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data; //获取选中的数据
        switch (obj.event) {
            case 'add':
                let content = "create";
                _open('添加', content, width, height)
                break;
            case 'update':

                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else if (data.length > 1) {
                    layer.msg('只能同时编辑一个');
                } else {
                    let content = "update?id=" + data[0].id;
                    _open('编辑', content, width, height)
                }
                break;
            case 'delete':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else {
                    let ids = "";
                    for (let field of data) {
                        ids += field.id + ","
                    }
                    layer.confirm('真的删除行么', function (index) {
                        //向服务端发送删除指令
                        let url = "{:url('delete')}";
                        let params = {'id': ids};
                        _del(url, params, null, table)
                    });
                }
                break;
        }
    }


    // +---------------------------------------------
    // ajax提交
    // +---------------------------------------------
    function _axios(url, params, method = 'post',table) {
        axios({
            url: url,
            method: method,
            data: params
        }).then(res => {
            if (res.data.code == 1) {
                layer.msg(res.data.msg, {icon: 1}, function () {
                    window.parent.location.reload();//刷新父页面
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    //window.parent.location.reload();
                    //window.location.reload();
                })

            } else {
                layer.msg(res.data.msg, {icon: 5})
            }
        });
    }


    // +---------------------------------------------
    // 删除
    // +---------------------------------------------
    function _del(url, params, obj, table) {
        axios({
            url: url,
            method: 'post',
            data: params
        }).then(res => {
            if (res.data.code == 1) {
                layer.msg(res.data.msg, {icon: 1}, function () {
                    if (obj == null) {
                        table.reload('testReload', {});
                    } else {
                        obj.del();
                    }
                })
            } else {
                layer.msg(res.data.msg, {icon: 5})
            }
        });
    }


    // +---------------------------------------------
    // 弹出层
    // +---------------------------------------------
    function _open(title, content, width, height) {
        layer.open({
            type: 2,
            area: [width, height],
            shadeClose: true,
            title: title,
            maxmin: true,
            Boolean: true,
            content: content
        });
    }
</script>